using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public static class Tool
{
    /// <summary>
    /// 生成8位随机种子字符串（大小写字母组合）
    /// </summary>
    public static string GenerateRandomSeedString()
    {
        int length = 8;
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        char[] result = new char[length];

        long ticks = DateTime.UtcNow.Ticks;
        int hash = (int)(ticks ^ (ticks >> 32));

        for (int i = 0; i < length; i++)
        {
            hash = hash * 1664525 + 1013904223;
            result[i] = chars[Math.Abs(hash) % chars.Length];
        }

        return new string(result);
    }

    /// <summary>
    /// 通过种子、调用次数和概率获取伪随机结果
    /// </summary>
    /// <param name="seed">随机种子（字符串）</param>
    /// <param name="callCount">调用次数（用于区分同种子下的不同随机场景）</param>
    /// <param name="probability">成功概率（0-1之间，例如0.3表示30%）</param>
    /// <returns>是否命中概率事件（true/false）</returns>
    public static bool GetRandom(float probability)
    {
        string seed = InfoSystem.Instance.seed;
        int callCount = InfoSystem.Instance.callCount;
        // 1. 种子字符串转哈希值（确保同种子生成相同初始值）
        int seedHash = seed.GetHashCode();

        // 2. 结合调用次数生成唯一哈希（同种子不同调用次数产生不同随机值）
        int combinedHash = seedHash ^ callCount;

        // 3. 线性同余算法生成伪随机数（确保结果可复现）
        combinedHash = combinedHash * 1664525 + 1013904223; // 同种子生成逻辑的算法参数，保证一致性
        int randomValue = Math.Abs(combinedHash);

        // 4. 将随机值映射到0-1范围，与目标概率比较
        float normalized = (randomValue % 10000) / 10000f; // 取模后归一化到0-1

        InfoSystem.Instance.callCount += 1;
        // Debug.Log($"normalized: {normalized}, callCount: {callCount}");
        return normalized <= probability;
    }
    public static void ResetTool()
    {
        InfoSystem.Instance.seed = GenerateRandomSeedString(); // 生成新的种子
        InfoSystem.Instance.callCount = 0;                        // 调用次数归零
    }

}